package com.neusoft.yuejian26.yuejian_26.Activity;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.neusoft.yuejian26.yuejian_26.Bean.Userbean;
import com.neusoft.yuejian26.yuejian_26.Other.CacheInfo;
import com.neusoft.yuejian26.yuejian_26.R;
import com.neusoft.yuejian26.yuejian_26.model.UserModel;
import com.neusoft.yuejian26.yuejian_26.network.RetrofitCallback;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;

import static android.R.id.input;
import static com.neusoft.yuejian26.yuejian_26.Utils.Data.uid;

public class QRActivity extends AppCompatActivity {

    public ImageView user_pic,my_qr,svg_back;
    public TextView my_name;
    public String uid=null;
    private Userbean userData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr);
        initView();
        initEvent();
        getUserIntro();
    }

    private Bitmap generateBitmap(String content,int width, int height) {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, String> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        try {
            BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    if (encode.get(j, i)) {
                        pixels[i * width + j] = 0x00000000;
                    } else {
                        pixels[i * width + j] = 0xffffffff;
                    }
                }
            }
            return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void generate() {
        Bitmap qrBitmap = generateBitmap(userData.getUname(),800, 800);
        my_qr.setImageBitmap(qrBitmap);
    }


    private void getUserIntro() {
        uid=new CacheInfo(QRActivity.this).getUserIdCache();
        UserModel userModel = new UserModel();
        Call call = userModel.userIntro(uid);
        userModel.bindCallback(call, new RetrofitCallback() {
            @Override
            public void onResponse(Object o) {
                userData = (Userbean) o;
                Picasso.with(QRActivity.this)
                        .load("http://115.159.93.244/new18yuejian/Public/upload/" + userData.getUserpic())
                        .into(user_pic);
                my_name.setText(userData.getUname());
                generate();
            }
        });

    }

    private void initView() {
        user_pic= (ImageView) findViewById(R.id.user_pic);
        my_name= (TextView) findViewById(R.id.my_name);
        my_qr= (ImageView) findViewById(R.id.my_qr);
        svg_back= (ImageView) findViewById(R.id.svg_back);
    }

    private void initEvent() {
        svg_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

}
